import math
import numpy as np
import cv2
def cal_rectangle(hand_landmarks):
        # 计算边界框的最小和最大坐标
        min_x = min([landmark.x for landmark in hand_landmarks.landmark])
        min_y = min([landmark.y for landmark in hand_landmarks.landmark])
        max_x = max([landmark.x for landmark in hand_landmarks.landmark])
        max_y = max([landmark.y for landmark in hand_landmarks.landmark])
        return min_x, min_y, max_x, max_y
def cal_relative(hand_landmarks):
    xy_dis = []
    min_x, min_y, max_x, max_y = cal_rectangle(hand_landmarks)
    scaled_x = abs(max_x - min_x)
    scaled_y = abs(max_y - min_y)
    xyz = hand_landmarks.landmark
    for idx in range(len(xyz)):
        if idx == len(xyz) - 1:
            x_dis = math.sqrt(((xyz[idx].x - min_x) / scaled_x) ** 2 + ((xyz[0].x - min_x) / scaled_x) ** 2)
            y_dis = math.sqrt(((xyz[idx].y - min_y) / scaled_y) ** 2 + ((xyz[0].y - min_y) / scaled_y) ** 2)
            xy_dis.append([x_dis, (xyz[idx].x - min_x) / scaled_x, y_dis, (xyz[idx].y - min_y) / scaled_y])
        else:
            x_dis = math.sqrt(((xyz[idx + 1].x - min_x) / scaled_x) ** 2 + ((xyz[idx].x  - min_x) / scaled_x) ** 2)
            y_dis = math.sqrt(((xyz[idx + 1].y - min_y) / scaled_y) ** 2 + ((xyz[idx].y - min_y) / scaled_y) ** 2)
            xy_dis.append([x_dis, (xyz[idx].x - min_x) / scaled_x, y_dis, (xyz[idx].y - min_y) / scaled_y])
    xy_dis = np.array(xy_dis)
    return xy_dis
